<?php
namespace Home\Controller;
class IndexController extends HomeController {
    public function index(){
        $this->assign('title','虎啸网');
        $first = M('user')->field('u.name, a.title, a.create_time, a.zan,i.img,u.motto, c.cname, c.id cid,a.id aid,u.id uid')
            ->table('hx_user u, hx_image i, hx_article a, hx_category c')
            ->where('u.id=a.user_id AND i.art_id=a.id AND c.id=a.cate_id AND a.status=1')
            ->limit(1)
            ->order('a.id desc')
            ->select();

        $second = M('user')->field('u.name, a.title, a.create_time, a.zan,i.img,u.motto, c.cname, c.id cid,a.id aid,u.id uid')
            ->table('hx_user u, hx_image i, hx_article a, hx_category c')
            ->where('u.id=a.user_id AND i.art_id=a.id AND c.id=a.cate_id AND a.status=1')
            ->limit(1,2)
            ->order('a.id desc')
            ->select();

        $data = M('user')->field('u.name, a.title, a.create_time, a.time, a.zan,i.img,u.motto, c.cname, c.id cid,a.id aid,u.id uid, h.img_40')
            ->table('hx_user u, hx_image i, hx_article a, hx_category c, hx_headimg h')
            ->where('u.id=a.user_id AND i.art_id=a.id AND c.id=a.cate_id AND h.id=u.headimg_id AND a.status=1')
            ->limit(3,2)
            ->order('a.id desc')
            ->select();

        // $ids = M('article')->field('id')->select();
        // // v($ids);exit;
        // $c = array();
        // foreach ($ids as $key => $v) {
        //     $a = M('comment')->field('count("*") total, a.status')->table('hx_article a, hx_comment c')->where('c.art_id=a.id AND  a.id='.$v['id'])->order('a.id desc')
        //                 // ->fetchSql(true)
        //                 ->select();
        //     if ($a[0]['status'] == 1) {
        //         unset($a[0]['status']);
        //         $c[] = $a[0];
        //     }
        //     v($a);
        // }
        
        // v($c);
        // exit;
        $short = M('short')->where('cate_id=4')->order('id desc')->limit(10)->select();

        $hot = M('category')
                ->field('a.title, i.img, a.id aid')
                ->table('hx_article a, hx_image i, hx_user u')
                ->where('u.id=a.user_id AND i.art_id=a.id AND a.status=1')
                ->order('a.read_times desc')
                ->limit(3)
                ->select();

        // v($first);
        // v($second);
        // v($data);exit;
        // v($short);exit;

        $this->news();

        $this->assign('first', $first[0]);
        $this->assign('second', $second);
        $this->assign('data', $data);
        $this->assign('short', $short);
        $this->assign('hot', $hot);
        $this->display('Index/index');
    }

    //ajax加载主页
    public function indexAjax()
    {
        $m = $_POST['m'];
        $limit = (3+2*$m).",2";

        $list = M('user')->field('u.name, a.title, a.create_time, a.time, a.zan,i.img,u.motto, c.cname, c.id cid,a.id aid,u.id uid, h.img_40')
            ->table('hx_user u, hx_image i, hx_article a, hx_category c, hx_headimg h')
            ->where('u.id=a.user_id AND i.art_id=a.id AND c.id=a.cate_id AND h.id=u.headimg_id AND a.status=1')
            ->limit($limit)
            ->order('a.id desc')
            ->select();

        if (empty($list)) {
            $list = null;
        }
        $m = 0;
        foreach ($list as $key => $val) {
            $list[$m]['time'] = date('Y-m-d H:i:s', $val['time']);
            $m++;
        }
        $this->ajaxReturn($list);
    }

    //短趣ajax加载
    // public function shortAjax()
    // {
    //     echo $a = $_POST['a'];
    //     // $limit = 
    //     // $short = M('article')->where('cate_id=6')->order('id desc')->limit(5)->select();
    //     $this->ajaxReturn($a);
    // }

    //新闻API
    public function news()
    {
        $ch = curl_init();
        $url = 'http://apis.baidu.com/songshuxiansheng/news/news';
        $header = array(
            'apikey: 3d60743ce50ccc68daa82e39cd6f58f4',
        );
        // 添加apikey到header
        curl_setopt($ch, CURLOPT_HTTPHEADER  , $header);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // 执行HTTP请求
        curl_setopt($ch , CURLOPT_URL , $url);
        $res = curl_exec($ch);

        $news = json_decode($res)->retData;
        $this->assign('news', $news);
        // v($news);exit;
    }
}
